<?php

	session_start();
	//Inclusion des fichiers
	require_once $_SERVER['DOCUMENT_ROOT'].'/site-cba/stul_config.php';
	require_once $a_fmConnect;
	require_once $a_fAdminFonct;
	require_once $a_fmSql;
	require_once $a_fmEscape;
	require_once $_SERVER['DOCUMENT_ROOT'].'/site-cba/controller/controle_upload_image.php';
	

	//Reception des GET et traitement
	if(isset($_GET["mode"]))
	{
		switch ($_GET["mode"]) {
			case 'delArticle':
				sql_delete_post($_GET["id"]);
				echo '<script language="Javascript">document.location.replace("./viewer/index.php?mode=editArticles");</script>';
			break;
			case 'delCompte':
				sql_delete_user($_GET["id"]);
				echo '<script language="Javascript">document.location.replace("./viewer/index.php?mode=editComptes");</script>';
			break;
			case 'delCom':
				sql_delete_com($_GET["id"]);
				echo '<script language="Javascript">document.location.replace("./viewer/index.php?mode=editComs");</script>';
			break;
			case 'delCat':
				sql_delete_cat($_GET["id"]);
				echo '<script language="Javascript">document.location.replace("./viewer/index.php?mode=editCats");</script>';
			break;
			
			default:break;
		}
	}

	//Reception des POST et traitement
	if(isset($_POST["action"]))
	{
		switch ($_POST["action"]) {
			case 'Se connecter':
					checkLoginAdmin( $_POST['login'], $_POST['password']);
				break;
			case 'logout':
					unset($_SESSION["login"]);
					unset($_SESSION["pass"]);
					unset($_SESSION["keyAdmin"]);
					unset($_SESSION["adminAuth"]);
					unset($_SESSION["id"]);
					echo '<script language="Javascript">document.location.replace(".");</script>';
				break;

			case 'Mettre à jour':
				$tab_img = image_a_ajouter_au_post($_POST,$rootSite);
				if(isset($_SESSION['erreur_upload']))
				{
					echo '<script language="Javascript">history.go(-1);</script>';
				}
				else
				{
					sql_edit_post($_POST,$tab_img['file'],$tab_img['type']);
					unset($_SESSION['dest']);				
					echo '<script language="Javascript">document.location.replace("./viewer/index.php?mode=editArticles");</script>';
				}
			break;

			case 'Mettre à jour le commentaire':
				sql_edit_com($_POST);
				echo '<script language="Javascript">document.location.replace("./viewer/index.php?mode=editComs");</script>';
			break;

			case 'Mettre à jour le compte':
				sql_allEdit_user($_POST["id_user"], $_POST["login"], $_POST["password"], $_POST["pseudo"], $_POST["email"], $_POST["status"]);
				echo '<script language="Javascript">document.location.replace("./viewer/index.php?mode=editComptes");</script>';
			break;

			case 'Mettre à jour la categorie':
				sql_edit_cat_of_idCat($_POST["id_cat"], $_POST["content"], $_POST["title"]);
				echo '<script language="Javascript">document.location.replace("./viewer/index.php?mode=editCats");</script>';
			break;

			case 'Ajouter ce compte':
				sql_inscrire_user_by_admin($_POST["login"], $_POST["password"], $_POST["pseudo"], $_POST["email"], $_POST["dateReg"], $_POST["status"]);
				//La redirection ce fera dans action, si oui ou non le login et valide
				//echo '<script language="Javascript">document.location.replace("./viewer/index.php?mode=editComptes");</script>';
			break;

			case 'Ajouter cette categorie':
				sql_add_cat($_POST["name"], $_POST["desc"]);
				//La redirection ce fera dans action, si oui ou non le login et valide
				//echo '<script language="Javascript">document.location.replace("./viewer/index.php?mode=editComptes");</script>';
			break;
			case "Ajouter article":
				//print_r($_POST);
				//addArticle($_POST['content'], $_SESSION["idUser"],$_POST['title'], $_POST['tags'], $_POST['category']) or die(mysql_error());	
				//MatHack: Il faudra rajouter le champs de la categorie plus tard
				//$_POST['category']
				$tab_img = image_a_ajouter_au_post($_POST,$rootSite);
				if(isset($_SESSION['erreur_upload']))
				{
					echo '<script language="Javascript">history.go(-1);</script>';
				}
				else
				{
					addArticle($_POST['content'], $_SESSION["id"], $_POST['title'], $_POST['tags'], $_POST['category'],$tab_img['file'],$tab_img['type']);
					unset($_SESSION['dest']);				
					echo '<script language="Javascript">document.location.replace("./viewer/index.php?mode=editArticles");</script>';
				}				
				//header('Location:./viewer/index.php?mode=editArticles');
			break;

			default:break;
		}
	}

function image_a_ajouter_au_post($_POST,$rootSite)
{
	$tab_img = array();
	unset($_SESSION['erreur_upload']);
	if($_POST['image'] == "image_up")
	{
		$upload1 = upload('fichier',$rootSite."avatars/",15360000000, array('png','gif','jpg','jpeg') );
		if($upload1 === true)
		{
            if(img_exist($_SESSION['dest']['dir'].$_SESSION['dest']['filename']) != false)
            {
            	unlink($_SESSION['dest']['dir'].$_SESSION['dest']['filename']);
            	$tab_img['file'] = "existe";
            	$tab_img['type'] = $_SESSION['dest']['filename'];
            }
            else
            {
            	$tab_img['file'] = $_SESSION['dest'];
            	$tab_img['type'] = false;            	
            }
		}
		else
		{
			$_SESSION['erreur_upload'] = $upload1;
		}
	}
	else if($_POST['image'] == "image_default")
	{
        $tab_img['file'] = "default";
        $tab_img['type'] = "default"; 
	}
	else if($_POST['image'] == "image_existante")
	{
        $tab_img['file'] = "existe";
        $tab_img['type'] = $_POST['image_bdd'];
	}
	return $tab_img;
}
?>